package edu.intprog.egreeting;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.intprog.configuration.ConfigurationManager;
import edu.intprog.egreeting.basic.EgreetingBaseServlet;
import edu.intprog.egreeting.beans.Greeting;
import edu.intprog.egreeting.beans.UserDef;

/**
 * Servlet implementation class for Servlet: ReshowGreetingServlet
 *
 */
 public class ReshowGreetingServlet extends EgreetingBaseServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ReshowGreetingServlet() {
		super();
	}   	 	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDef user = (UserDef) request.getSession().getAttribute("user");
		if( user==null)
		{
			//simple security measure
			getServletContext().getRequestDispatcher("/StartServlet").forward(request, response);
			return;
		}
		
		//fill the greeting fields from the request
		Greeting greeting = new Greeting();
		greeting.set_senderName( (String) request.getParameter("senderName") );
		greeting.set_senderEmail( (String) request.getParameter("senderEmail") );
		greeting.set_recipientName( (String) request.getParameter("recipientName") );
		greeting.set_recipientEmail( (String) request.getParameter("recipientEmail") );
		greeting.set_backgroundImage( (String) request.getParameter("backImage") );
		greeting.set_backgroundSound( (String) request.getParameter("backMusic") );
		greeting.set_personalBlessing( (String) request.getParameter("personalBlessing") );
		greeting.set_blessing( (String) request.getParameter("blessing") );
		greeting.set_title( (String) request.getParameter("title") );
		greeting.set_greetingImage( (String) request.getParameter("foreImage") );
		
		if (request.getParameter("preview")!=null)
		{
			//preview was pressed
			ConfigurationManager.fillRequestWithGreetingEssentials(request);
			ConfigurationManager.prepareGreetingForPreview(greeting, request);
			getServletContext().getRequestDispatcher("/greetingPreview.jsp").forward(request, response);
			return;
		}
		
		//probably Resend was pressed - forward to the new greeting page
		//update the greeting so it will seems as if the current user is the sender
		String userName = user.get_fName() + " " + user.get_lName();
		greeting.set_senderName(userName ) ;
		greeting.set_senderEmail( user.get_emailAddr() );
		greeting.set_recipientName( "" );
		greeting.set_recipientEmail( "" );
		request.setAttribute("greetingTemplate", greeting);

		//add the greeting required data into the request
		ConfigurationManager.fillRequestWithGreetingEssentials(request);
		
		getServletContext().getRequestDispatcher("/greetingTemplate.jsp").forward(request, response);
		return;
	}   	  	    
}